package com.echoesnet.echoesshortvideo.views;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.echoesnet.echoesshortvideo.R;
import com.orhanobut.logger.Logger;

import java.util.Random;

/**
 * @author yqh
 * @Date 2018/5/2
 * @Version 1.0
 */
public class LikeHeartLayout extends FrameLayout
{
    private final String TAG = LikeHeartLayout.class.getSimpleName();

    private Drawable icon;
    private Context context;
    private GestureDetector gestureDetector;

    public LikeHeartLayout(@NonNull Context context)
    {
        this(context, null);
    }

    public LikeHeartLayout(@NonNull Context context, @Nullable AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public LikeHeartLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context)
    {
        this.context = context;
        icon = context.getResources().getDrawable(R.drawable.but_big_heart_activate);
        setClickable(true);
        gestureDetector = new GestureDetector(context, new GestureListener());
        setOnTouchListener(new OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                gestureDetector.onTouchEvent(event);
                return false;
            }
        });
    }

    private class GestureListener extends GestureDetector.SimpleOnGestureListener
    {

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e)
        {
            Logger.t(TAG).d("Click------->onSingleTapConfirmed()");
            if (actionListener != null)
                actionListener.onClick();
            return super.onSingleTapConfirmed(e);
        }

        @Override
        public boolean onDoubleTap(MotionEvent e)
        {
            Logger.t(TAG).d("Click------->onDoubleTap()");
            addHeartView(e.getX(), e.getY());
            if (actionListener != null)
                actionListener.onDoubleClick();
            return false;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e)
        {
            Logger.t(TAG).d("Click------->onDoubleTapEvent()");
//            addHeartView(e.getX(), e.getY());
            return false;
        }
    }


    private void addHeartView(float x, float y)
    {
        LayoutParams params = new LayoutParams(icon.getIntrinsicWidth() + 50, icon.getIntrinsicHeight() + 50);//计算点击的点位红心的下部中间
        params.leftMargin = (int) (x - icon.getIntrinsicWidth() / 2);
        params.topMargin = (int) (y - icon.getIntrinsicHeight());
        ImageView imageView = new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        Matrix matrix = new Matrix();
        matrix.postRotate(getRandomRotate(), (icon.getIntrinsicWidth() / 2), (icon.getIntrinsicHeight() / 2)); //设置红心的微量偏移
        imageView.setImageMatrix(matrix);
        imageView.setImageDrawable(icon);
        imageView.setLayoutParams(params);
        addView(imageView);
        AnimatorSet animSet = getShowAnimSet(imageView);
        AnimatorSet cenSet = getCenterAnimSet(imageView);
        AnimatorSet hideSet = getHideAnimSet(imageView);
        animSet.start();
        animSet.addListener(new AnimatorListenerAdapter()
        {
            @Override
            public void onAnimationEnd(Animator animation)
            {
                super.onAnimationEnd(animation);
                cenSet.start();
            }
        });
        cenSet.addListener(new AnimatorListenerAdapter()
        {
            @Override
            public void onAnimationEnd(Animator animation)
            {
                super.onAnimationEnd(animation);

                new Handler().postDelayed(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        hideSet.start();
                    }
                }, 230);

            }
        });
        hideSet.addListener(new AnimatorListenerAdapter()
        {
            @Override
            public void onAnimationEnd(Animator animation)
            {
                super.onAnimationEnd(animation);
                removeView(imageView);
            }
        });

    }

    private AnimatorSet getShowAnimSet(ImageView imageView)
    {
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 1.5f, 0.8f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 1.5f, 0.8f);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(scaleX, scaleY);
        set.setDuration(80);
        return set;
    }

    private AnimatorSet getCenterAnimSet(ImageView imageView)
    {
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 0.7f, 1f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 0.7f, 1f);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(scaleX, scaleY);
        set.setDuration(60);
        return set;
    }


    private AnimatorSet getHideAnimSet(ImageView imageView)
    {
        // 1.alpha动画
        ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0.1f);
        // 2.缩放动画
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 1.8f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 1.8f);
        // 3.translation动画
        ObjectAnimator translation = ObjectAnimator.ofFloat(imageView, "translationY", 0f, -150f);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(alpha, scaleX, scaleY, translation);
        set.setDuration(500);
        return set;
    }


    /**
     * 生成一个随机的左右偏移量
     */
    private Float getRandomRotate()
    {
        int random = (new Random().nextInt(20) - 10);
        return random + 0.0f;
    }

    private OnViewClickActionListener actionListener;

    public interface OnViewClickActionListener
    {
        void onClick();

        void onDoubleClick();
    }

    public void setOnViewClickActionListener(OnViewClickActionListener listener)
    {
        actionListener = listener;
    }

}
